package com.movie.mybatis.service.impl;

import com.movie.mybatis.mapper.MovieMapper;
import com.movie.mybatis.mapper.MovieRecordMapper;
import com.movie.mybatis.mapper.UserRecordMapper;
import com.movie.mybatis.pojo.Movie;
import com.movie.mybatis.pojo.MovieRecord;
import com.movie.mybatis.pojo.User;
import com.movie.mybatis.pojo.UserRecord;
import com.movie.mybatis.service.MovieUser;
import com.movie.mybatis.utils.SqlSessionUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class MovieUserServlet extends HttpServlet implements MovieUser {

    public void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.openSession();
            MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
            List<Movie> movies = mapper.selectAll();
            request.setAttribute("movieList", movies);
        }catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        request.getRequestDispatcher("/recordlist.jsp").forward(request, response);
    }

    public void doLook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        SqlSession sqlSession = null;
        try {
            //记录开始观看的时间
            Long startTime = System.currentTimeMillis();
            HttpSession session = request.getSession();
            session.setAttribute("startTime", startTime);
            sqlSession = SqlSessionUtil.openSession();
            MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
            String movieId = request.getParameter("movieId");
            Movie movie = mapper.selectByIdtoMovieRecord(Integer.parseInt(movieId));
            request.setAttribute("movie", movie);
        }catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        request.getRequestDispatcher("/look.jsp").forward(request, response);
    }

    public void dosave(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SqlSession sqlSession = null;
        try {
            HttpSession session = request.getSession();
            String movieName = request.getParameter("moviename");
            User user = (User) session.getAttribute("user");

            Long endTime = System.currentTimeMillis();
            Long startTime = (Long) session.getAttribute("startTime");
            //获取用户观看时间
            Long elapsedTime = (endTime - startTime) / 1000;
            String comment = request.getParameter("comment");

            String userName = user.getUserName();

            sqlSession = SqlSessionUtil.openSession();

            if (!comment.equals("")) {
                //电影评论
                MovieRecord movieRecord = new MovieRecord(movieName, comment);
                MovieRecordMapper mapper = sqlSession.getMapper(MovieRecordMapper.class);
                mapper.insertMovieRecord(movieRecord);
            }
            UserRecord userRecord = new UserRecord(userName, movieName, String.valueOf(elapsedTime), comment);
            UserRecordMapper mapperUser = sqlSession.getMapper(UserRecordMapper.class);
            mapperUser.insertUserRecord(userRecord);

            sqlSession.commit();

        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        response.sendRedirect(request.getContextPath() + "/record/list");
    }

    public void doRecordDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = null;
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");

            sqlSession = SqlSessionUtil.openSession();
            UserRecordMapper mapper = sqlSession.getMapper(UserRecordMapper.class);
            List<UserRecord> userRecords = mapper.selectAllRecordByUserName(user.getUserName());
            request.setAttribute("userRecords", userRecords);

        }catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        request.getRequestDispatcher("/recorddetail.jsp").forward(request, response);
    }
}


